// Copyright (c) 2010 Marcos David Marín Amador <marcosmarin@gmail.com>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// return array of key-value pairs
function cfaqs_get_params() {
	var i1 = 0;
	var i2 = 0;
	var query = location.search;
	var result = Array();
	
	while (i2 < query.length) {
		i1 = i2 + 1;
		i2 = query.indexOf("=", i1);
		
		if (i2 == -1) {
			break;
		}
		
		var key = query.substring(i1, i2);
		i1 = i2 + 1;
		i2 = query.indexOf("&", i1);
		
		if (i2 == -1) {
			i2 = query.length;
		}
		
		var val = query.substring(i1, i2);
		result[key] = val;
	}
	
	return result;
}

// works with new URLs
function cfaqs_get_board_and_topic() {
	var arr = location.pathname.split("/");
	var board = arr[2].substring(0, arr[2].indexOf("-"));
	var topic = arr[3];
	
	if (topic && topic.indexOf("#") != -1) {
		topic = topic.substring(0, topic.indexOf("#"));
	}
	
	arr = new Array();
	arr["board"] = board;
	arr["topic"] = topic;
	
	return arr;
}

var cfaqs_page_board_list = "board-list";
var cfaqs_page_topic_list = "topic-list";
var cfaqs_page_message_list = "message-list";
var cfaqs_page_message_list_archived = "message-list-archived";
var cfaqs_page_message_detail = "message-detail";
var cfaqs_page_message_preview = "message-preview";
var cfaqs_page_message_posted = "message-posted";
var cfaqs_page_topic_preview = "topic-preview";
var cfaqs_page_unknown = "unknown-page";

// figure out which page we are currently viewing
function cfaqs_get_current_page() {
	function is_defined(v) {
		if (typeof(v) == "undefined") {
			return false;
		}
		
		return true;
	}
	
	function is_topic_unavaliable_page() {
		var boardwrap = document.getElementById("board_wrap");
		if (boardwrap.firstChild.innerText == "The topic you selected is no longer available for viewing.") {
			return true;
		}
		
		return false;
	}
	
	function is_topic_deleted_page() {
		var boardwrap = document.getElementById("board_wrap");
		if (boardwrap.firstChild.innerText == "Your topic has been deleted") {
			return true;
		}
		
		return false;
	}

	function is_message_deleted_page() {
		var boardwrap = document.getElementById("board_wrap");
		if (boardwrap.firstChild.innerText == "Your message has been deleted") {
			return true;
		}
		
		return false;
	}

	function is_message_unavailable_page() {
		var boardwrap = document.getElementById("board_wrap");
		if (boardwrap.firstChild.innerText == "The message you selected is no longer available for viewing.") {
			return true;
		}
		
		return false;
	}

	function is_message_posted_page() {
		var boardwrap = document.getElementById("board_wrap");
		var text = boardwrap.innerText;
		
		if (text.indexOf("You should be returned to the Message List automatically in five seconds.") == -1) {
			return false;
		}
		
		return true;
	}

	function is_archived_topic() {
		var boardwrap = document.getElementById("board_wrap");
		var text = boardwrap.firstChild.nextSibling.innerText;
		
		if (text.indexOf("Topic Archived") != -1) {
			return true;
		}
		
		return false;
	}
	
	function pathname_depth() {
		var i = 0;
		var len = location.pathname.length;
		var depth = 0;
		
		for (i = 0; i < len; i++) {
			if (location.pathname[i] == '/') {
				depth++;
			}
		}
		
		// last char could be a meaningless '/'
		if (location.pathname[location.pathname.length-1] == '/') {
			depth--;
		}
		
		return depth;
	}

	var resource = location.pathname.match(/.*\/(.+\..+)$/);
	
	if (resource) {
		resource = resource[1];
	}
	
	var params = cfaqs_get_params();
	var depth = pathname_depth();
	
	switch (depth) {
	case 1:
		return cfaqs_page_board_list;
	case 2:
		switch (resource) {
		case "user.php":
		case "settings.php":
		case "contact.php":
			return cfaqs_page_unknown;
		case "post.php":
			if (is_message_posted_page()) {
				return cfaqs_page_message_posted;
			}
			
			if (is_defined(params["board"]) &&
				is_defined(params["topic"])) {
				return cfaqs_page_message_preview;
			}
			
			if (is_defined(params["board"])) {
				return cfaqs_page_topic_preview;
			}
		default:
			return cfaqs_page_topic_list;
		}
	case 3:
		if (is_topic_unavaliable_page()) {
			return cfaqs_page_topic_list;
		}
		
		if (is_archived_topic()) {
			return cfaqs_page_message_list_archived;
		}
		
		return cfaqs_page_message_list;
	case 4:
		if (is_topic_deleted_page()) {
			return cfaqs_page_topic_list;
		}
	
		if (is_message_deleted_page()) {
			return cfaqs_page_message_list;
		}
		
		if (is_defined(params["action"])) {
			return cfaqs_page_message_list;
		}
		
		return cfaqs_page_message_detail;
	}
	
	return cfaqs_page_unknown;
}
